<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$item=array('type'=>'0','status'=>'1','diy'=>';ordersn;address;realname;mobile;remark;','diygoods'=>';productsn;title;total;');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'自定义导出',
		'edit'=>array(
			array('type'=>'radio','title'=>'导出类型','name'=>'type','options'=>'type'),
			array('type'=>'string','title'=>'订单号','name'=>'ordersn'),
			array('type'=>'radio','title'=>'订单状态','name'=>'status','options'=>'status'),
			array('type'=>'radio','title'=>'支付方式','name'=>'paytype','options'=>'paytype'),
			array('type'=>'radio','title'=>'时间类型','name'=>'timetype','options'=>'timetype'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time_range','show'=>array('timetype'=>0)),
			array('name'=>'paytime','title'=>'支付时间','type'=>'time_range','show'=>array('timetype'=>1)),
			array('type'=>'checkbox','title'=>'自定义导出数据','name'=>'diy','options'=>'diy'),
			array('type'=>'checkbox','title'=>'商品导出数据','name'=>'diygoods','options'=>'diygoods','show'=>array('type'=>1)),
		)
	);
	$return['type']=array('0'=>'按订单导出','1'=>'按商品导出');
	$return['timetype']=array('0'=>'按下单时间','1'=>'按支付时间');
	$return['paytype']=array('wechat'=>'微信支付','credit'=>'余额支付');
	$return['status']=array('0'=>'全部订单','1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$return['diygoods']=array('goodssn'=>'编码','productsn'=>'国家商品条码','offcode'=>'商品称码','title'=>'商品名称','optionid'=>'规格','unit'=>'单位','price'=>'单价','total'=>'数量');
	$return['diy']=array('price'=>'实收费用','ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注','invoice'=>'发票信息','data2'=>'表单内容');
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();	
	$_SESSION['e2cpost']=$post;
	$url=download_url('jy_merch/order.export/down');
	exi('点确定开始下载','','',$url);
	
}
if($_RQ['op']=='down'){
	$f=array('price'=>'实收费用','ordersn'=>'订单号','nickname'=>'买家昵称','phone'=>'买家电话','mid'=>'会员id','address'=>'收货地址','realname'=>'收货人','mobile'=>'收货电话','goods'=>'商品信息','paytype'=>'支付方式','goodsprice'=>'商品小计','discountprice'=>'运费','createtime'=>'下单时间','paytime'=>'付款时间','sendtime'=>'发货时间','finishtime'=>'订单完成时间','express'=>'快递公司','expresssn'=>'快递单号','remark'=>'订单备注','invoice'=>'发票信息','data2'=>'表单内容');
	$gf=array('goodssn'=>'编码','productsn'=>'国家商品条码','offcode'=>'商品称码','title'=>'商品名称','optionid'=>'规格','unit'=>'单位','price'=>'单价','total'=>'数量');
	$diy=str_array($_SESSION['e2cpost']['diy']);
	$diygoods=str_array($_SESSION['e2cpost']['diygoods']);
	$type=$_SESSION['e2cpost']['type'];
	$header=array();
	if($type==1){
		foreach($diygoods as $gv){
			if($gv){
				$header[$gv]=$gf[$gv];
			}
		}
	}
	foreach($diy as $v){
		if($v){
			$header[$v]=$f[$v];
		}
	}
	if(!empty($_SESSION['e2cpost']['ordersn'])){
		$params['ordersn']=trim($_SESSION['e2cpost']['ordersn']);
	}
	if(!empty($_SESSION['e2cpost']['status'])){
		$params['status']=intval($_SESSION['e2cpost']['status']);
	}
	if(!empty($_SESSION['e2cpost']['paytype'])){
		$params['paytype']=intval($_SESSION['e2cpost']['paytype']);
	}
	if(!$_SESSION['e2cpost']['timetype']){
		//创建时间
		if($_SESSION['e2cpost']['createtime']){
			//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
			//$time_range=json_decode($time_range,true);
			$time_range=$_SESSION['e2cpost']['createtime'];
			$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		}
	}else{
		//支付时间
		if($_SESSION['e2cpost']['paytime']){
			//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
			//$time_range=json_decode($time_range,true);
			$time_range=$_SESSION['e2cpost']['paytime'];
			$params['paytime between']=array($time_range['tstart'],$time_range['tend']);
		}
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$params['merchid >']=0;
	$list=pdo_getall('jy_weishop_order',$params);
	if(count($list)>5000){
		exi('订单数量大于5000，请修改搜索条件！','error');
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	if($type==1){
		$exdata=array();
	}
	$express_types=model('jy_weishop/model')->express_types();
	foreach($list as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');
		$v['phone']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'mobile');
		$v['address']=json_decode($v['address'],true);
		if($v['address'] && $v['address']['name']){	
			$address=$v['address'];
			$addressid=$v['address'];
		}else{
			$address=pdo_get('core_address',array('id'=>$v['addressid']));
			$addressid=$v['addressid'];
		}
		$v['address']=model('core/member')->get_address_str($addressid);
		$v['address']=str_replace(',','，',$v['address']);
		$v['address']=str_replace('\n','，',$v['address']);
		$v['remark']=str_replace(',','，',$v['remark']);
		$v['remark']=str_replace('\n','，',$v['remark']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['name'];
		$q=$v['status'];
		$v['status']=$status[$q];
		$w=$v['paytype'];
		$v['paytype']=$paytype[$w];
		$e=$v['ispay'];
		$v['ispay']=$ispay[$e];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['paytime']=$v['paytime']?date('Y-m-d H:i:s',$v['paytime']):'';
		$v['sendtime']=date('Y-m-d H:i:s',$v['sendtime']);
		$v['finishtime']=date('Y-m-d H:i:s',$v['finishtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$v['diyformdata']=json_decode($v['diyformdata'],true);
		$v['data2']='';
		if(is_array($v['diyformdata'])){
			foreach($v['diyformdata'] as $dk=>&$diyv){
					$v['data2'] .='【'.$diyv['title']."】：";
					if(is_array($diyv['data']) && $diyv['type']!=7){
						$diyv['data']=$diyv['data'][0].' '.$diyv['data'][1].' '.$diyv['data'][2].' '.$diyv['data'][3];
					}
					if($diyv['type']==7){
						$diyv['data']=$diyv['data']['province'].' '.$diyv['data']['city'].' '.$diyv['data']['district'];
					}
					$v['data2'] .=$diyv['data'];
			}
		}
		$invoice=pdo_get('jy_weishop_order_invoice',array('orderid'=>$v['id']));
		if($invoice){
			$invoicetype=$invoice['type']==2?'增值税专用发票':'电子普通发票';
			$v['invoice']='['.$invoicetype.'] 抬头：'.$invoice['name'].' 税号：'.$invoice['code'].' 邮箱：'.$invoice['email'];
		}
		$express=pdo_get('jy_weishop_order_express',array('orderid'=>$v['id']));
		$v['express']=$express_types[$express['express_id']]['name'];
		$v['expresssn']=$express['expresssn'];
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('optionid','goodsid','price','total'));
		foreach($goods as $k=>$gv){
				$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid']),array('title','goodssn','productsn','offcode','unit'));
				$goodsinfo['title']=str_replace(',',' ',$goodsinfo['title']);
				$goodstitle=$goodsinfo['title'];
				//商品规格
				if($gv['optionid']){
						$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs','goodssn','productsn'));
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$selespename=implode("，",$specs);
				}else{
					$options=array();
					$selespename='无规格';
				}
				$goodsinfo['goodssn']=$options['goodssn']?:$goodsinfo['goodssn'];
				$goodsinfo['productsn']=$options['productsn']?:$goodsinfo['productsn'];
				if($type==1){
					$newv=array();
					$newv['title']=$goodsinfo['title'];
					$newv['optionid']=$selespename;
					$newv['goodssn']=$goodsinfo['goodssn']?:'无';
					$newv['productsn']=$goodsinfo['productsn']?:'无';
					$newv['offcode']=$goodsinfo['offcode']?:'无';
					$newv['unit']=$goodsinfo['unit']?:'件';
					$newv['price']=$gv['price'];
					$newv['total']=$gv['total'];
					$newv=array_merge($v,$newv);
					$exdata[]=$newv;
				}else{
					$num=$k+1;
					$v['goods'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
				}
		}
	}
	$exdata=$type==1?$exdata:$list;
	cfc('tools')->export2excel($header,$exdata,'订单');
}